<script setup lang="ts">
import { getCurrentInstance, ref, onBeforeMount } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
const { proxy } = getCurrentInstance()

/* ---- */
const WXConfig = proxy.$Store.get('WXConfig') || {}

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const areas = [{
  pageType: 1,
  name: '业绩提成',
  count: 0,
  page: 'WageSent',
  title: '发放业绩提成',
  // ----
  wages: [{
    salary: 0,
    type: 'post',
    url: 'salary-log/balance',
    data: {
      status: 0,
      is_level: 1
    }
  }, {
    salary: 0,
    type: 'post',
    url: 'salary-log/balance',
    data: {
      status: 1,
      is_level: 1
    }
  }],
  // ----
  type: 'get',
  url: 'salary-log/index',
  data: {
    params: {
      'SalaryLogSearch[status]': 0,
      'SalaryLogSearch[is_level]': 1
    }
  }
}, {
  pageType: 2,
  name: '团队奖金',
  count: 0,
  page: 'WageSent',
  title: '发放团队奖金',
  // ----
  wages: [{
    salary: 0,
    type: 'post',
    url: 'salary-log/balance',
    data: {
      status: 0,
      is_level: 2
    }
  }, {
    salary: 0,
    type: 'post',
    url: 'salary-log/balance',
    data: {
      status: 1,
      is_level: 2
    }
  }],
  // ----
  type: 'get',
  url: 'salary-log/index',
  data: {
    params: {
      'SalaryLogSearch[status]': 0,
      'SalaryLogSearch[is_level]': 2
    }
  }
}, {
  pageType: 3,
  name: '代理佣金',
  count: 0,
  page: 'WageSent',
  title: '发放代理佣金',
  // ----
  wages: [{
    salary: 0,
    type: 'post',
    url: 'salary-log/balance',
    data: {
      status: 0,
      is_level: 3
    }
  }, {
    salary: 0,
    type: 'post',
    url: 'salary-log/balance',
    data: {
      status: 1,
      is_level: 3
    }
  }],
  // ----
  type: 'get',
  url: 'salary-log/index',
  data: {
    params: {
      'SalaryLogSearch[status]': 0,
      'SalaryLogSearch[is_level]': 3
    }
  }
}]
// ----
const items = ref([])

/* ---- */
const layer = ref()

/* ---- */
const getCurrencyInfo = async () => {
  // --获取对应数量--
  for (let i = 0; i < Number(areas.length); i++) {
    await proxy.$http[areas[i].type](areas[i].url, areas[i].data).then((res) => {
      areas[i].count = res._meta.totalCount
    }).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
  }
  // ----
  areas.forEach((item) => {
    items.value.push({
      show: true,
      child: [{
        show: true,
        isArrow: true,
        name: item.name + '（' + item.count + '条）',
        callback: () => {
          const forExtend = item.wages
          proxy.$Myfun.openPage(proxy, 'push', item.page, { pageType: item.pageType, title: item.title, exTend: JSON.stringify(forExtend) })
        }
      }]
    })
  })
}
// ----
const getAuditFun = (type, item) => {
  switch (Number(type)) {
    case 1: {
      item.callback && item.callback(item)
      break
    }
  }
}

/* ---- */
onBeforeMount(() => {
  getCurrencyInfo()
})
onBeforeRouteLeave(() => { proxy.$Myfun.setScrollTop(proxy.$refs.formSection) })
</script>

<template>
  <div class="mescroll-touch" ref="formSection">
    <ul class="userSys xw-flex xw-flex-column">
      <template v-for="(item, index) in items">
      <li v-if="item.show">
        <template v-for="(son, num) in item.child">
        <a class="xw-Tfg xw-flex xw-flex-align" @click="getAuditFun(1, son)" v-if="son.show">
          <div class="USS-title xw-flex-W1">{{son.name}}</div>
          <i class="fal fa-angle-right" v-if="son.isArrow"></i>
        </a>
        </template>
      </li>
      </template>
    </ul>
    <layer-view ref="layer"/>
  </div>
</template>
